package com.wash.shoes.controller.pc;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.common.enums.JdEnum;
import com.wash.shoes.domain.dto.ReserveDto;
import com.wash.shoes.domain.jd.JdCreateOrder;
import com.wash.shoes.domain.mtbusy.MtUpdateFulFillInfo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.OrderTransportPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.OrderTransportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wangxy
 * @date 2024/9/24 21:23
 */
@Log4j2
@Api(tags = {"PC-商家处理订单"})
@RestController
@RequestMapping({"/merchant", "/dev-api/merchant"})
public class MtMerchantController {

    /**
     * 订单服务
     */
    @Autowired
    OrderService orderService;

    @Autowired
    OrderTransportService orderTransportService;

    @Autowired
    MtUpdateFulFillInfo mtUpdateFulFillInfo;

    @Autowired
    JdCreateOrder jdCreateOrder;


    @ApiOperation("美团的商家改约")
    @PostMapping("/merchantModify")
    public R<String> merchantModify(@Validated @RequestBody ReserveDto reserveDto) {

        OrderTransportPo orderTransportPo = orderTransportService.getOneByOrderId(reserveDto.getOrderId());
        // 只有已经下发和空单中可以修改物流状态
        if (Integer.parseInt(orderTransportPo.getStatus()) <= JdEnum.ORDER_KONG_DZ.getStatus()) {

            OrderPo orderPo = orderService.getById(reserveDto.getOrderId());
            if(StringUtils.equals(orderPo.getComeFrom(),"2")) {
                // 美团更新,出错跑出异常
                mtUpdateFulFillInfo.merchantModify(reserveDto);
            }
            // 京东更新，出错抛出异常
            jdCreateOrder.commonModifyOrder(reserveDto);
            // 更新mt的时间
            Date date = new Date(reserveDto.getBookStartTime());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String dateStr = format.format(date);
            orderPo.setMtBeginTime(dateStr);
            orderService.updateById(orderPo);
        } else {
            throw new WashException("揽收后订单不能改约");
        }
        return R.ok("Suc");
    }

}
